#queue.py
#authur: codesz
#email: codesz.th@gmail.com
#version: 0.01
#date: 8 october 2009
#time: 11:27


class QueueException(Exception):
    def __init__(self):
        pass
    def __str__(self):
        return 'Queue: key or value cannot be None.'


class qnode:
    def __init__(self,key=None,value=None):
        self.prev=self.next=None
        self.key=key
        self.value=value
        
    def eq(self, node):
        return self.key == node.key        
        
class queue:
    def __init__(self):
        self.head=None
        self.tail=None
        self.count=0
        
    def empty(self):
        return self.head==None and self.tail==None

    def count(self):
        return self.count
    
    def push(self,key=None,value=None):
        node=qnode(key,value)
        if key is None or value is None:
            raise QueueException
        if self.head is None or self.tail is None:
            self.head=self.tail=qnode(key,value)
        else:
            self.tail.next=qnode(key,value)
            ptr=self.tail
            self.tail=ptr.next
            self.tail.prev=ptr
            
            self.prev=ptr
            
        self.count+=1
        
        return self.tail
    
    def pop(self):
        if self.head is None or self.tail is None: return None
        ptr=self.head
        self.head=ptr.next
        self.count+=1
        return ptr.key,ptr.value
        
    def __str__(self):
        s='[%d]'%self.count
        ptr=self.head
        while 1:
            s+='%s:%s, '%(ptr.key,ptr.value)
            if ptr.next is None: break
            ptr=ptr.next
            
        return s[:-2]


if __name__=='__main__':
    q=queue()
    q.push(1,'One')
    q.push(2,'Two')
    q.push('Three',3)
    print q
    print 'pop', q.pop()
    print q
    q.push(4,'Four')
    print q
    q.push()
